package ch1;

/**
 * Created by tantexian on 2016/10/22.
 */
/**
 *
 * @author tantexian
 * @since 2016/10/22
 * @params
 */
public class ch1_2 {
    public static void main(String[] args) {
        char[] chars = {'a','b','c','d','t','t','x','\n'};
        printChars(chars);
        reverse(chars);
        printChars(chars);
    }

    // 逆转字符串数组(最后一个字符位/n,保持不变)
    public static void reverse(char[] chars) {
        // 最后一个字符‘\n’，位置不改变
        int end = chars.length - 2;
        int start = 0;
        char tmp;
        while (start <= end) {
            tmp = chars[start];
            chars[start] = chars[end];
            chars[end] = tmp;
            start ++;
            end --;
        }
    }

    private static void printChars(char[] chars) {
        for (char ch : chars) {
            if(ch == '\n'){
                System.out.print("\\n");
            }else{
                System.out.print(ch);
            }
        }
        System.out.println();
    }
}
